package com.sjx.sjxlibrary.log;

/**
 * Created by sjx on 2022/7/13
 * 日志堆栈格式化
 */
class HiStackTraceFormatter implements HiLogFormatter<StackTraceElement[]> {

    @Override
    public String format(StackTraceElement[] stackTraceElement) {
        StringBuilder builder = new StringBuilder(128);
        if (stackTraceElement == null || stackTraceElement.length == 0) {
            return null;
        } else if (stackTraceElement.length == 1) {
            return "\t-" + stackTraceElement[0].toString();
        } else {
            for (int i = 0, len = stackTraceElement.length; i < len; i++) {
                if (i == 0) {
                    builder.append("stackTrace: \n");
                }
                if (i != len - 1) {
                    builder.append("\t├ ");
                    builder.append(stackTraceElement[i].toString());
                    /* java中\t是一个tab键的效果、\n是一个换行的效果 */
                    builder.append("\n");
                } else {
                    builder.append("\t└ ");
                    builder.append(stackTraceElement[i].toString());
                }
            }
            return builder.toString();
        }
    }
}
